package solution;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-19
 * Time: 17:09
 */

//https://leetcode.cn/problems/reverse-only-letters/submissions/396302894/
class Solution5 {
    public String reverseOnlyLetters(String s) {
        char[] arr = s.toCharArray();
        int left = 0;
        int right = arr.length - 1;
        while (left < right) {
            while (left < right && !Character.isLetter(arr[left])) {
                left++;
            }
            while (left < right && !Character.isLetter(arr[right])) {
                right--;
            }
            if (left < right) {
                char temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left++;
                right--;
            }
        }
        return String.copyValueOf(arr);
        //return new String(arr);//貌似更好
    }
}
